package org.example.likou150.array;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/*
* https://leetcode.cn/problems/is-subsequence/?envType=study-plan-v2&envId=top-interview-150
* */
public class IsSubsequence {
    public static void main(String[] args) {
        String s = "abc";
        String t = "ahbgdc";
        IsSubsequence isSubsequence=new IsSubsequence();
        boolean subsequence = isSubsequence.isSubsequence(s, t);
        System.out.println(subsequence);
    }

    public boolean isSubsequence(String s, String t) {
        char[] sCharArray = s.toCharArray();
        char[] tCharArray = t.toCharArray();
        int tIndex=0;
        for (int i = 0; i < tCharArray.length; i++) {
            if(tIndex==s.length()){
                return true;
            }
            if(tCharArray[i]==sCharArray[tIndex]){
                tIndex++;
            }
        }
        return tIndex==s.length();
    }
    
    
//    public boolean isSubsequence(String s, String t) {
//        Set<Character> tSet=new HashSet<>();
//        char[] tArray = t.toCharArray();
//        for (int i = 0; i < tArray.length; i++) {
//            tSet.add(tArray[i]);
//        }
//        char[] charArray = s.toCharArray();
//        for (int i = 0; i < charArray.length; i++) {
//            if(!tSet.contains(charArray[i])){
//                return false;
//            }
//        }
//        return true;
//    }
}
